package org.example.cet.demos.controller;

import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.dto.PaperDto;
import org.example.cet.demos.domain.dto.QuestionDto;
import org.example.cet.demos.domain.dto.QuestionsDto;
import org.example.cet.demos.domain.form.QuestionsForm;
import org.example.cet.demos.domain.po.Questions;
import org.example.cet.demos.domain.query.QuestionsQuery;
import org.example.cet.demos.service.impl.QuestionsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/questions")
public class QuestionsController {
    @Autowired
    private QuestionsServiceImpl questionsService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody QuestionsForm questionsForm) {
        Questions questions = converter.ToQuestions(questionsForm);
        questionsService.save(questions);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        questionsService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @Valid @RequestBody QuestionsForm questionsForm) {
        Questions questions = converter.ToQuestions(questionsForm);
        questions.setId(id);
        questionsService.updateById(questions);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(QuestionsQuery questionsQuery) {
        PageBean pb = questionsService.getPage(questionsQuery);
        return Result.success(pb);
    }

    @GetMapping("/getTypeList/{type}")
    public Result getTypeList(@PathVariable int type) {
        List<Map<String,Object>> questions = questionsService.getTypeList(type);
        return Result.success(questions);
    }

    @GetMapping("/getTypeTitleList/{type}")
    public Result getTypeTitleList(@PathVariable int type) {
        List<Questions> questions=questionsService.getTypeTitleList(type);
        return Result.success(questions);
    }

    @GetMapping("/getQuestion/{id}")
    public Result getQuestion(@PathVariable Integer id) {
        QuestionDto question = questionsService.getQuestion(id);
        return Result.success(question);
    }
}
